package com.zktr.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.sun.org.apache.xpath.internal.operations.Bool;
import com.sun.scenario.effect.Bloom;
import com.zktr.bean.AdminBean;
import com.zktr.dao.AdminDao;
import com.zktr.pojo.Admin;

public class AdminService {
	AdminDao aDao = new AdminDao();
	public static void main(String[] args) {
		AdminService adminService = new AdminService();
	//	System.out.println(adminService.loginVerification("刘强东","123456"));
		System.out.println(adminService.deleteOneAdmin(8));
	}
	public List<AdminBean> loginVerification(String username,String password) {
		  //  Boolean flag=true;
		ArrayList<AdminBean> arrayList = new ArrayList<>();
		 List<Admin> loginVerification = aDao.loginVerification(username, password);
		 findAllAdmin(loginVerification,arrayList);
		 
		  return arrayList;
		    //isEmpty() 判断IsEmpty()是Java中用于判断某种容器是否有元素的系统库函数。如用来判断ArrayList，HashSet，HashMap是否有元素等
		    //有的话返回一个Boolean类型的值false
		    /*if (lVerification.isEmpty()) {
				return !flag;
			}else {
				return flag;
			}*/
		}
	public boolean loginVerificationUsername(String username) {
	    Boolean flag=true;
	    List<Admin> lVerification = aDao.loginVerificationUsername(username);
	    //isEmpty() 判断IsEmpty()是Java中用于判断某种容器是否有元素的系统库函数。如用来判断ArrayList，HashSet，HashMap是否有元素等
	    //有的话返回一个Boolean类型的值false
	    if (lVerification.isEmpty()) {
			return !flag;
		}else {
			return flag;
		}
	}
	public List<AdminBean> queryAllAdmin() {
		List<Admin> qA = aDao.queryAllAdmin();
		List<AdminBean> arrayList = new ArrayList<>();
		findAllAdmin(qA,arrayList);
        return arrayList;
	}
	private static String mydate(int addtime) {
		Calendar cal = Calendar.getInstance();
		long millions = new Long(addtime).longValue() * 1000;
		cal.setTimeInMillis(millions);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		return df.format(cal.getTime());
	}
	 private static void findAllAdmin(List<Admin> list, List<AdminBean> list2) {
			for (Admin admin : list) {
				AdminBean aBean = new AdminBean();
		        aBean.setRoleid(admin.getRoleid()+"");
		        aBean.setUserid(admin.getUserid()+"");
		        aBean.setUsername(admin.getUsername());
		        aBean.setPassword(admin.getPassword());
		        aBean.setAddtime(mydate(admin.getAddtime())+"");
		        aBean.setEmail(admin.getEmail());
		        aBean.setRolelist(admin.getRolelist());
		        if (admin.getLastlogin()==0) {
		         aBean.setLastlogin("暂无");
				}else {
					aBean.setLastlogin(mydate(admin.getLastlogin()));
				}
		        
		        aBean.setPortrait(admin.getPortrait());
				list2.add(aBean);
			}
	   }
	 public int queryAllAdmin(Admin admin)  {
		return aDao.insertOneAdmin(admin);
			
		}
/*	 public List<Admin> selectAdminOne(String name){
		 
	 }*/
	 public int updateOnePortrait(AdminBean adBean,String id) {
		return aDao.updateAdmin(adBean, id);
	}
	 public int updateOneAll(AdminBean adBean,String id) {
		return aDao.updateGoods(adBean, id);
	}
		public boolean deleteOneAdmin(int id) {
			 Boolean flag=true;
             int deleteOneAdmin = aDao.deleteOneAdmin(id);
             if (deleteOneAdmin>0) {
				return flag;
			}else {
				return !flag;
			}
			
		}
}
